package org.lex.ftp.queue;

import java.util.*;

public class ListContainer<E> extends AbstractQueueContainer<E> implements IQueueContainer<E> {
	public static <E> ListContainer<E> vector() {
		return new ListContainer<E>(new Vector<E>());
	}

	public static <E> ListContainer<E> linkedList() {
		return new ListContainer<E>(new LinkedList<E>());
	}

	public static <E> ListContainer<E> arrayList() {
		return new ListContainer<E>(new ArrayList<E>());
	}

	private final List<E> list;

	public ListContainer(final List<E> list) {
		this.list = list;
	}

	public void addFirst(E... elements) {
		if (null == elements)
			return;
		for (int i = elements.length - 1; i >= 0; i--)
			this.list.add(0, elements[i]);
	}

	public void addLast(E... elements) {
		for (E e : elements)
			this.list.add(e);
	}

	public void clear() {
		list.clear();
	}

	public E getFirst() {
		return list.get(0);
	}

	public E get(int index) {
		return list.get(index);
	}

	public void set(int index, E e) {
		list.set(index, e);
	}

	public int size() {
		return list.size();
	}

	public boolean isEmpty() {
		return list.isEmpty();
	}

	public Iterator<E> iterator() {
		return list.iterator();
	}

	public E removeFirst() {
		return list.remove(0);
	}

	public void remove(int[] indexes) {
		this.list.removeAll(this.getElements(indexes));
	}

	private Collection<E> getElements(int[] indexes) {
		Arrays.sort(indexes);
		int index = 0;
		Iterator<E> iter = this.iterator();
		ArrayList<E> tmp = new ArrayList<E>(indexes.length);
		while (iter.hasNext()) {
			E e = iter.next();
			if (Arrays.binarySearch(indexes, index) >= 0)
				tmp.add(e);
			index++;
		}
		return tmp;
	}

	public String toString() {
		return list.toString();
	}
}
